package net.onecloud.onecloud.employ;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import net.onecloud.onecloud.R;
import net.onecloud.onecloud.account.resume.AddResumePage;
import net.onecloud.onecloud.adapter.SelectResumeAdapter;
import net.onecloud.onecloud.application.App;
import net.onecloud.onecloud.base.BaseAppCompatActivity;
import net.onecloud.onecloud.model.ResultResume;
import net.onecloud.onecloud.model.ResultUser;
import net.onecloud.onecloud.network.NetWork;
import net.onecloud.onecloud.utils.LogUtils;
import net.onecloud.onecloud.utils.RecycleViewDivider;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class SelectResumeActivity extends BaseAppCompatActivity implements BaseQuickAdapter.OnRecyclerViewItemChildClickListener {


    private RecyclerView mRecyclerView;
    private SelectResumeAdapter mSelectResumeAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    Observer<ResultResume> queryResumeObserver = new Observer<ResultResume>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            LogUtils.e(e);
        }

        @Override
        public void onNext(ResultResume resultResume) {
            if (resultResume.getSuccess() == 1) {
                mSelectResumeAdapter.setNewData(resultResume.getData());
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resume);
        setToolBarTitle("我的简历");
        HideLeftButton(false);
        HideRightButton(false);
        initViews();
        initListener();
        initData();
    }

    private void initData() {


        Observable.just(App.getUser())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new Func1<ResultUser.DataEntity, Observable<ResultResume>>() {
                    @Override
                    public Observable<ResultResume> call(ResultUser.DataEntity dataEntity) {
                        if (dataEntity != null) {
                            return NetWork.getResumeApi()
                                    .query1(dataEntity.getId())
                                    .subscribeOn(Schedulers.io());
                        }
                        showHit("请先登录");
                        return null;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(queryResumeObserver);
    }

    private void initListener() {
        mSelectResumeAdapter.setOnRecyclerViewItemChildClickListener(this);
    }

    private void initViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.m_recyclerview);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL));
        mSelectResumeAdapter = new SelectResumeAdapter();
        mRecyclerView.setAdapter(mSelectResumeAdapter);

    }

    @Override
    public void onClickRightButton() {
        Intent intent = new Intent(SelectResumeActivity.this, AddResumePage.class);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                ResultResume.DataEntity entity = data.getExtras().getParcelable("resume");
                if (entity != null) {
                    mSelectResumeAdapter.add(0, entity);
                    mRecyclerView.scrollToPosition(0);
                }
            }
        }
    }


    @Override
    public void onItemChildClick(final BaseQuickAdapter baseQuickAdapter, View view, final int i) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定投递该简历?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ResultResume.DataEntity resume = (ResultResume.DataEntity) baseQuickAdapter.getItem(i);
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("resume", resume);
                        intent.putExtras(bundle);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }).setNegativeButton("取消", null)
                .show();

    }
}
